﻿/******************************************************************
** 文 件 名: Bu_DocReceive_List.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 1:28:04
** 描    述: 资料接收表列表页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using Honestysoft.Financing.WebUI.Module.Contract;
using System.Data.Common;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
	public partial class Bu_DocReceive_List : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {		
				AspNetPager1.PageSize = Globals.PageSize;
				BindControl();
                Bind();
            }
        }
		/// <summary>
        /// 绑定页面控件。
        /// </summary>
		private void BindControl()
		{
            ControlHelper.BindDictionary(ddlContractCategory, "ContractCategory");
            ControlHelper.InsertAllItemAndSelect(ref ddlContractCategory);
            btnAdd.Enabled = false;
            btnFirst.Visible = false;
            pnlSum.Visible = false;
            Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
            switch (nodeType)
            {
                case Utils.ContractTreeNodeType.Root:
                    //btnFirst.Enabled = true;
                    break;
                case Utils.ContractTreeNodeType.ProjectCategory://项目分类
                    //btnFirst.Enabled = true;
                    break;
                case Utils.ContractTreeNodeType.Project://项目
                    txtProjectName.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(Request.QueryString["ID"]);
                    txtProjectName.Enabled = false;
                    btnFirst.Enabled = true;
                    break;
                case Utils.ContractTreeNodeType.ContractCategory://合同类别
                    txtProjectName.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(Request.QueryString["ParentID"]);
                    txtProjectName.Enabled = false;
                    ddlContractCategory.SelectedValue = Request.QueryString["ID"];
                    ddlContractCategory.Enabled = false;
                    break;
                case Utils.ContractTreeNodeType.Contract:
                    BD_Contract contract = new Honestysoft.BaseObject.BaseBusinessLogic<BD_Contract>(Globals.CurrentConnStringName).GetEntity(Request.QueryString["ID"]);
                    if (contract != null)
                    {
                        txtProjectName.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(contract.ProjectID);
                        txtProjectName.Enabled = false;
                        ddlContractCategory.SelectedValue = contract.ContractCategory;
                        ddlContractCategory.Enabled = false;
                        txtCode.Text = contract.Code;
                        txtCode.Enabled = false;
                        txtName.Text = contract.Name;
                        txtName.Enabled = false;
                    }
                    btnAdd.Enabled = true;
                    pnlSum.Visible = true;
                    break;
                default:
                    break;
            }
		}
		/// <summary>
        /// 设置弹出窗体大小
		/// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
		protected override void SetDialogSize()
		{
			base.SetDialogSize();
			//重载宽度，高度
            //ModalDialogSize.EditHeight = 700;
            //ModalDialogSize.EditWidth = 900;
            //ModalDialogSize.ViewHeight = 700;
            //ModalDialogSize.ViewWidth = 900;
		}
		/// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                btnAdd.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/Bu_DocReceive_Edit.aspx?IsNew=1&FunctionID=" + FunctionID + "&ContractID=" + Request.QueryString["ID"] + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表');return false;");
                //Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
                //switch (nodeType)
                //{
                //    case Utils.ContractTreeNodeType.Root:
                //        btnFirst.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/FirstProjDocReceive.aspx?IsNew=1&FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表-首次申请');return false;");
                //        break;
                //    case Utils.ContractTreeNodeType.ProjectCategory://项目分类
                //        btnFirst.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/FirstProjDocReceive.aspx?IsNew=1&FunctionID=" + FunctionID + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表-首次申请');return false;");
                //        break;
                //    case Utils.ContractTreeNodeType.Project://项目
                //        btnFirst.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/FirstDocReceive.aspx?IsNew=1&FunctionID=" + FunctionID + "&ProjectID=" + Request.QueryString["ID"] + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表-首次申请');return false;");
                //        break;                    
                //    default:
                //        break;
                //}
                //btnFirst.Attributes.Add("onclick", "javascript:openDialog('" + Globals.Home + "Module/Audit/FirstDocReceive.aspx?IsNew=1&FunctionID=" + FunctionID + "&ProjectID=" + Request.QueryString["ID"] + "'," + ModalDialogSize.EditWidth + ", " + ModalDialogSize.EditHeight + ", '新增资料接收表-首次申请');return false;");
            }
        }
		/// <summary>
        /// 绑定列表
        /// </summary>
		private void Bind()
        {
            Honestysoft.Financing.BusinessLogic.Audit bll = new BusinessLogic.Audit(Globals.CurrentConnStringName);
            Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(Request.QueryString[Utils.ParamName_ContractNodeType]);
            string projectCategory = "";
            string projectId = "", projectName = "";
            string contractCategory = ddlContractCategory.SelectedValue;
            if (contractCategory == "0") { contractCategory = null; }
            string code = txtCode.Text.Trim();
            if (code.Length == 0) { code = null; }
            string name = txtName.Text.Trim();
            if (name.Length == 0) { name = null; }
            string id = "";
            if (txtProjectName.Enabled)
            {
                if (txtProjectName.Text.Trim().Length > 0)
                {
                    projectName = txtProjectName.Text.Trim();
                }
            }
            switch (nodeType)
            {
                case Utils.ContractTreeNodeType.Root:
                    break;
                case Utils.ContractTreeNodeType.ProjectCategory://项目分类
                    projectCategory = Request.QueryString["ID"];
                    projectId = null;
                    break;
                case Utils.ContractTreeNodeType.Project://项目
                    projectCategory = Request.QueryString["ParentID"];
                    projectId = Request.QueryString["ID"];
                    break;
                case Utils.ContractTreeNodeType.ContractCategory://合同类别
                    projectId = Request.QueryString["ParentID"];
                    contractCategory = Request.QueryString["ID"];
                    break;
                case Utils.ContractTreeNodeType.Contract://合同
                    projectCategory = "";
                    projectId = "";
                    contractCategory = "";
                    name = "";
                    code = "";
                    id = Request.QueryString["ID"];
                    //DataTable dtTemp = bll.GetSumAmountDocRecByContract(id);
                    DataTable dtTemp2 = bll.GetSumAmount_S_ByContract(id);
                    if (dtTemp2 != null && dtTemp2.Rows.Count == 1)
                    {
                        lblCount.Text = dtTemp2.Rows[0]["count_ID"].ToString();
                        if (dtTemp2.Rows[0]["Sum_Amount"] != null && dtTemp2.Rows[0]["Sum_Amount"] != DBNull.Value)
                        {
                            lblSum_Amount_DocRec.Text = string.Format("{0:N}", dtTemp2.Rows[0]["Sum_Amount"]);
                        }
                        else
                        {
                            lblSum_Amount_DocRec.Text = "0";
                        }
                    }
                    break;
                default:
                    break;
            }

            int pageCount = 0, recordCount = 0;
            DataTable dtList = bll.GetDocReceive(projectCategory, projectId, projectName, contractCategory, code, name, id, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtList;
            rptList.DataBind();
        }

        protected string GetCheckBox(string dataId, bool isUse)
        {
            if (isUse) { return ""; }
            return string.Format("<input type=\"checkbox\" name=\"chkID\" id=\"chk{0}\"  value=\"{0}\" >", dataId);
        }

		/// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId, bool isUse)
        {
            string script = "";
            if (!isUse)//如果已经导入并且派单了，是不能改的
            {
                if (PageSecurity.Edit)
                {
                    script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Audit/Bu_DocReceive_Edit.aspx", "修改资料接收表", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID, "&IsNew=0&IsUse=0", "", "", "");
                }
                if (PageSecurity.Delete)
                {
                    if (script.Length > 0) { script += "&nbsp;"; }
                    script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
                }
            }
            if (PageSecurity.View)
            {
                if (script.Length > 0) { script += "&nbsp;"; }
                script += ScriptHelper.LinkButton.GetGridViewButton(Globals.Home + "Module/Audit/DocReceivePrint.aspx", "查看资料接收表", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID, "&IsNew=0&IsUse=" + (isUse ? "1" : "0"), "", "", "");
            }
            return script;
        }
		
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }
		
		protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
		/// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"]+"";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(Globals.CurrentConnStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                string[] arrId = GetSelectedRowKeys();
                List<string> lsID = new List<string>();
                lsID.AddRange(arrId);
                new Honestysoft.Workflow.BusinessFacade.WFApply.FlowManage(Globals.CurrentConnStringName).DeleteWorkflow(((int)EnumItem.WFBill.AuditTable).ToString(), lsID, transaction);
                new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).DeleteDocReceive(arrId, transaction);
                transaction.Commit();
                ScriptHelper.Window.Alert("删除成功！");
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
            finally
            {
                connection.Close();
            }
        }
	}
}
